﻿using FC.ERP.TMom.Api.AppHandlers.Commands.EssentialDatas.FactoryManages.WorkShops;
using FC.ERP.TMom.Api.DTOs;
using FC.ERP.TMom.Domain;
using FC.ERP.TMom.ErrorCode;
using FC.ERP.TMom.Infrastructure.Interfaces;
using MediatR;

namespace FC.ERP.TMom.Api.AppHandlers.CommandHandlers.EssentialDatas.FactoryManages.WorkShops
{
    public class WorkShopQuertyCommandHandler : IRequestHandler<WorkShopQuertyCommand, APIResult<APIPaging<WorkShopDTO>>>
    {
        private readonly IBaseRepository<WorkShop> workShoprepository;
        public WorkShopQuertyCommandHandler(IBaseRepository<WorkShop> workShoprepository)
        {
            this.workShoprepository = workShoprepository;
        }

        public Task<APIResult<APIPaging<WorkShopDTO>>> Handle(WorkShopQuertyCommand request, CancellationToken cancellationToken)
        {
            APIResult<APIPaging<WorkShopDTO>> result=new APIResult<APIPaging<WorkShopDTO>>();
            result.Code=ResultCode.查询成功;
            result.Message=ResultCode.查询成功.ToString();
            var list = from a in workShoprepository.GetAll()
                       where !a.IsDeleted
                       select new WorkShopDTO
                       {
                           WorkShopId=a.WorkShopId,
                           WorkShopCode=a.WorkShopCode,
                           WorkShopName=a.WorkShopName,
                           Description=a.Description,
                           CreateId=a.CreateId,
                           CreateTime=a.CreateTime,
                           UpdateTime=a.UpdateTime,
                       };
            int totalCount=list.Count();
            int totalPage=(int)Math.Ceiling(totalCount*1.0/request.PageSize);
            list = list.Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize);
            APIPaging< WorkShopDTO > entity=new APIPaging<WorkShopDTO>();
            entity.TotalCount=totalCount;
            entity.TotalPage=totalPage;
            entity.List=list.ToList();
            result.Data=entity;

            return Task.FromResult(result);
        }
    }
}
